package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqSolidliquidProduceEquipmentRunningMapper;
import com.guofen.pojo.SjqSolidliquidProduceElectricity;
import com.guofen.pojo.SjqSolidliquidProduceEquipmentRunning;
import com.guofen.service.sjq.SjqSolidliquidProduceEquipmentRunningService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqSolidliquidProduceEquipmentRunningServiceImpl implements SjqSolidliquidProduceEquipmentRunningService {

    @Autowired
    private SjqSolidliquidProduceEquipmentRunningMapper sjqSolidliquidProduceEquipmentRunningMapper;

    @Override
    public boolean addOnTime(String equipment_name, String operator) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        String time = DateAndTimeUtil.getTime();

        //判断是否能根据班次和设备名称查询到当天对应数据，若能，则更新添加onTime2,若不能，则新建对象添加onTime1
        String shift;
        int result = 0;
        try {
            shift = DateAndTimeUtil.getShiftLabel(time);
            SjqSolidliquidProduceEquipmentRunning record = sjqSolidliquidProduceEquipmentRunningMapper.getLast(shift, equipment_name);
            if (record != null){
                record.setOnTime2(time);
                String name = record.getOperator();
                if (!name.contains(operator))
                    record.setOperator(name+" "+operator);
                result = sjqSolidliquidProduceEquipmentRunningMapper.updateByPrimaryKeySelective(record);
            }else {
                SjqSolidliquidProduceEquipmentRunning sjqSolidliquidProduceEquipmentRunning = new SjqSolidliquidProduceEquipmentRunning();
                sjqSolidliquidProduceEquipmentRunning.setDate(date);
                sjqSolidliquidProduceEquipmentRunning.setShift(shift);
                sjqSolidliquidProduceEquipmentRunning.setEquipmentName(equipment_name);
                sjqSolidliquidProduceEquipmentRunning.setOnTime1(time);
                sjqSolidliquidProduceEquipmentRunning.setOperator(operator);
                result = sjqSolidliquidProduceEquipmentRunningMapper.insert(sjqSolidliquidProduceEquipmentRunning);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result == 1;
    }

    @Override
    public boolean addOffTime(String equipment_name, String operator) {
        int result = 0;
        String time = DateAndTimeUtil.getTime();

        //判断关机时间1/关机时间2
        String shift;
        try {
            shift = DateAndTimeUtil.getShiftLabel(time);
            SjqSolidliquidProduceEquipmentRunning record = sjqSolidliquidProduceEquipmentRunningMapper.getLast(shift, equipment_name);
            if (record == null)
                return false;
            if (record.getOffTime1() == null) {
                record.setOffTime1(time);
                String name = record.getOperator();
                if (!name.contains(operator))
                    record.setOperator(name+" "+operator);
                result = sjqSolidliquidProduceEquipmentRunningMapper.updateByPrimaryKeySelective(record);
            } else {
                record.setOffTime2(time);
                String name = record.getOperator();
                if (!name.contains(operator))
                    record.setOperator(name+" "+operator);
                result = sjqSolidliquidProduceEquipmentRunningMapper.updateByPrimaryKeySelective(record);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result == 1;
    }

    @Override
    public boolean addRemarks(String equipment_name, String remarks, String operator) {
        int result = 0;
        String time = DateAndTimeUtil.getTime();
        String shift;
        try {
            shift = DateAndTimeUtil.getShiftLabel(time);
            SjqSolidliquidProduceEquipmentRunning record = sjqSolidliquidProduceEquipmentRunningMapper.getLast(shift, equipment_name);
            if (record.getRemarks() == null)
                record.setRemarks(remarks);
            else
                record.setRemarks(record.getRemarks()+" "+remarks);
            result = sjqSolidliquidProduceEquipmentRunningMapper.updateByPrimaryKeySelective(record);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result == 1;
    }

    @Override
    public boolean addExaminationalForm(String equipment_name, String remark, String examiner) {
        int result = 0;
        String time = DateAndTimeUtil.getTime();
        String shift;
        try {
            shift = DateAndTimeUtil.getShiftLabel(time);
            SjqSolidliquidProduceEquipmentRunning record = sjqSolidliquidProduceEquipmentRunningMapper.getLast(shift, equipment_name);
            record.setExaminer(examiner);
            if (record.getRemarks() == null)
                record.setRemarks(remark);
            else if (!remark.equals(""))
                record.setRemarks(record.getRemarks()+"_"+remark);
            result = sjqSolidliquidProduceEquipmentRunningMapper.updateByPrimaryKeySelective(record);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result == 1;
    }

    @Override
    public List<SjqSolidliquidProduceEquipmentRunning> getListOnTheDay() {
        return sjqSolidliquidProduceEquipmentRunningMapper.getListOnTheDay();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqSolidliquidProduceEquipmentRunning> list = sjqSolidliquidProduceEquipmentRunningMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqSolidliquidProduceEquipmentRunning> list = sjqSolidliquidProduceEquipmentRunningMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqSolidliquidProduceEquipmentRunningMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqSolidliquidProduceEquipmentRunning record) {
        return sjqSolidliquidProduceEquipmentRunningMapper.updateByPrimaryKeySelective(record);
    }
}
